package cn.study.controller;


import cn.study.entity.StudyMenu;
import cn.study.entity.StudyTestPaper;
import cn.study.entity.StudyTestPaperParticulars;
import cn.study.service.StudyMenuService;
import cn.study.service.StudyTestPaperService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-07-20
 */
@Controller
@Slf4j
@RequestMapping("/study-menu")
public class StudyMenuController {
    @Resource
    StudyMenuService studyService;

    @GetMapping("/list")
    public String list(Model model, @RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize) {
        PageHelper.startPage(pn, paginalsize);
        List<StudyMenu> list = studyService.getlist();
        PageInfo pInfo = new PageInfo(list, 10);
        model.addAttribute("pInfo", pInfo);
        model.addAttribute("paginalsize", paginalsize);
        return "study_menulist";
    }
    @GetMapping("/addlist")
    public String addlist(Model model, @RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize
                        /*  @RequestParam(value="pageid",defaultValue="1") Integer pageid,
                          @RequestParam(value="pageid1",defaultValue="1") Integer pageid1*/) {
        List<StudyMenu> list = studyService.getlist();
        model.addAttribute("list", list);
       /* List<StudyMenu> list0 = studyService.getlist0();
        model.addAttribute("list0", list0);
        List<StudyMenu> list1 = studyService.getlist1();
        model.addAttribute("list1", list1);*/
        return "study_menu_add";
    }

    @GetMapping("/add")
    public String add(Model model,Integer menuParentId)
    {
        List<StudyMenu> list0 = studyService.getlist0();
        model.addAttribute("list0", list0);
        List<StudyMenu> list1 = studyService.getlist1();
        model.addAttribute("list1", list1);
        return "study_menu_add";
    }

    @PostMapping("/menuadd")
    public String deptadd(StudyMenu department){
        boolean bl=studyService.save(department);
        if(bl){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-menu/list";
    }
//    @GetMapping("/aaa")
//    public String list(Model model,@RequestParam(value="pageid",defaultValue="1") Integer pageid,
//                       @RequestParam(value="pageid1",defaultValue="1") Integer pageid1,int a) {
//        List<StudyMenu> list0 = studyService.getlist0();
//        model.addAttribute("list0", list0);
//        List<StudyMenu> list1 = studyService.getlist1();
//        model.addAttribute("list1", list1);
//        return "study_menu_add";
//    }
    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyService.removeById(id);
        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-menu/list";
    }
    @GetMapping("/upd")
    public String upd(Integer id,Model model)
    {
        StudyMenu studyDepartment=studyService.getById(id);
        model.addAttribute("list",studyDepartment);
        return "study_menu_upd";
    }

    @PostMapping("/menuupd")
    public String deptupd(Model model,StudyMenu department)
    {
        studyService.updateById(department);
        return "redirect:/study-menu/list";
    }



}

